package com.cupcorn.lib.display;

import org.andengine.entity.scene.Scene;

import com.cupcorn.lib.Utils;
import com.cupcorn.lib.game.GameAnimatedSprite;
import com.cupcorn.lib.game.GameEngine;
import com.cupcorn.lib.game.element.GameElementTiledSprite;

public class DisplayAnimatedSprite extends DisplayTiledSprite
{
	GameAnimatedSprite[] animatedSprite;
	
	public DisplayAnimatedSprite(GameEngine engine, GameElementTiledSprite[] gameElementTiledSprite)
	{
		super(engine, gameElementTiledSprite);
		
		animatedSprite = new GameAnimatedSprite[gameElementTiledSprite.length];
	}

	public GameAnimatedSprite newAnimatedSprite(int index)
	{
		return newAnimatedSprite(index, engine.getEngine().getScene());
	}
	
	public GameAnimatedSprite newAnimatedSprite(int index, Scene scene)
	{
		GameAnimatedSprite sprite = animatedSprite(index);
		scene.attachChild(sprite);
		return sprite;
	}
	
	public GameAnimatedSprite animatedSprite(int index)
	{
		float width = 0;
		float height = 0;
		
		if (gameElementTiledSprite[index].width != 0 && gameElementTiledSprite[index].height != 0)
		{
			width = Utils.getRatioWidth(gameElementTiledSprite[index].width, gameElementTiledSprite[index].ratio);
			height = Utils.getRatioHeight(gameElementTiledSprite[index].height, gameElementTiledSprite[index].ratio);
		}
		else
		{
			width = Utils.getRatioWidth(bitmap.getWidth() / gameElementTiledSprite[index].col, gameElementTiledSprite[index].ratio);
			height = Utils.getRatioHeight(bitmap.getHeight() / gameElementTiledSprite[index].row, gameElementTiledSprite[index].ratio);
			
		}
		float x = Utils.getPosX(gameElementTiledSprite[index].x, width, gameElementTiledSprite[index].anchor, gameElementTiledSprite[index].ratio);
		float y = Utils.getPosY(gameElementTiledSprite[index].y, height, gameElementTiledSprite[index].anchor, gameElementTiledSprite[index].ratio);
		animatedSprite[index] = new GameAnimatedSprite(x, y, width, height, textureRegion[index], engine.getVertexBufferObjectManager());
		return animatedSprite[index];
	}
	
	public GameAnimatedSprite getAnimatedSprite(int index)
	{
		return animatedSprite[index];
	}
}
